﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 巡检路线配置管理-树结构
    /// </summary>
    /// Title:ZMCL90
    /// Service:ServiceZMCL90
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2010-03-30  高强   [创建]
    ///
    public partial class ZMCL90 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        private string pathId;      //记录巡检路线id
        public ZMCL90()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCL05_Loaded);
        }

        /// <summary>
        /// 加载树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCL05_Loaded(object sender, RoutedEventArgs e)
        {
            this.surveyPathTree.Refresh();
        }

        /// <summary>
        /// 选择树节点，右侧页面刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void surveyPathTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.surveyPathTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(item);
            String level = args["level"];
            if (level.Equals("1"))
            {
                this.container.LoadView("ZMCL91", args);//巡检路线管理
            }
            else if (level.Equals("2"))
            {
                this.container.LoadView("ZMCL92", args);//巡检点管理
            }
            else if (level.Equals("3"))
            {
                args.Add("fdSecuritypathId", pathId);
                this.container.LoadView("ZMCL93", args);//安全检查项目管理
            }
        }

        /// <summary>
        /// 获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree">从EFTree获取节点</param>
        /// <returns></returns>
        private Dictionary<string, string> getTreeNodeInfo(EFTreeItem treeItem)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            _value = treeItem.ItemObject.GetType().GetProperty("text").GetValue(treeItem.ItemObject, null) as string;
            args.Add("fdParentId", _value);
            _value = treeItem.ItemObject.GetType().GetProperty("level").GetValue(treeItem.ItemObject, null) as string;
            args.Add("level", _value);
            if(_value.Equals("3"))
            {
                pathId = treeItem.ParentItem.DataContext.GetType().GetProperty("text").GetValue(treeItem.ParentItem.DataContext, null) as string;
            }
            _value = (treeItem.IsLeaf == true) ? "1" : "0";
            args.Add("isLeaf", _value);
            return args;
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("text").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("level").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("level", _value);
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;
                    args.Add("level", _value);
                }
            }
            BaseTools.RefreshTree("ZMCL90", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.surveyPathTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.surveyPathTree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            if (item.IsLeaf)
            {
                EFTreeItem parent = item.Parent as EFTreeItem;
                string node = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
                parent.Items.Clear();
                parent.CollapseItem();
                parent.ExpandItem();
                item = BaseTools.FindTreeItem(parent.Items, node, "text") as EFTreeItem;
                this.surveyPathTree.SelectedItem = item;
                return;
            }
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void surveyPathTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0)
            {
                this.surveyPathTree.SelectedItem = treeItem;
                treeItem.ExpandItem();//展开到二级节点
            }
        }
    }
}
